#!/usr/bin/perl
if (0 > $#ARGV){die "usage: eval.pl answerfile\n";}
$score=0;
$lines=0;
open (IN,$ARGV[0]);
while($answer=<IN>)
  {
  $answer=~tr/A-Z/a-z/;
  $i = 0;
  while($answer =~ m/(\S+)/g) {
    $answers[$i] = $1;
    $i++;
  }
  if ($answer=~/(\S+)/){$answer=$1;}
  if  ($attempt=<STDIN>)
    {
     $attempt =~tr/A-Z/a-z/;
     if ($attempt=~/(\S+)/){$attempt=$1;}
     $correct = 0;
     foreach $label (@answers) {
      $correct = 1 if($label eq $attempt);
     }
     #if ($answer eq $attempt) 
     if ($correct)
	{
	  $score++;
	  print sprintf("%-12s ..  CORRECT\n", $attempt);
	} 
     else
	{
	  print sprintf("%-12s ..  actually %s\n", $attempt, $answer);
	}
     $confarray{$attempt,$answer} += 1;
     $langs{$attempt} = 1;
     $langs{$answer} = 1;
     $lines++;
    }
  else {print "not enough lines in STDIN\n"; last;}
  }

$percent = $score * 100 / $lines;
print "\nScore $score right out of $lines";
print sprintf(" (%6.3f%%).\n", $percent);
if ($attempt=<STDIN>) {print "too many lines in STDIN\n";}

print "\nConfusions: (rows correct answer, columns guesses)\n\n\t";
foreach $key (sort(keys %langs))
  {
  $temp = substr($key, 0, 7);
  print "$temp\t";
  }
print "\n";

foreach $key (sort(keys %langs))
  {
  $temp = substr($key, 0, 7);
  print "$temp\t";
  foreach $key2 (sort(keys %langs))
    {
    if ($confarray{$key2,$key} > 0)
      {
      print "$confarray{$key2,$key}\t";
      }
    else
      {
      print "\t";
      }	
    }
  print "\n";
  }  

#foreach $key (sort(keys %confarray))
#  {
#  print "$key  $confarray{$key}\n";
#  }
